package com.aoxiang.zbox.model.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Data
@TableName("todo_item")
public class TodoItem {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String content;
    private String labels; // 存储为字符串格式，例如 "1,2,3"

    @TableField(value = "is_completed")
    private boolean completed;

    @TableLogic(value = "0", delval = "1")
    @TableField(value = "is_deleted")
    private boolean deleted;

    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    public List<Long> getLabelList() {
        if (labels == null || labels.isEmpty()) {
            return Collections.emptyList();
        }
        return Arrays.stream(labels.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
    }

    public void setLabelList(List<Long> labelList) {
        if (labelList == null || labelList.isEmpty()) {
            this.labels = "";
        } else {
            this.labels = labelList.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
        }
    }
}
